<?php
include_once 'Panel/UsersModel.php';
include_once 'PlatnosciModel.php';

class PlatnosciController extends PlatnosciModel{
	protected $user;
	protected $plID;   // indeks platnosci
	protected $db;
	private $view;	
	
	function __construct(){		
		$this->databaseSetup();		
	}
	
	/**
	 * Instalacja folderu, bazy płatności
	 * 
	 * @return unknown_type
	 */
	private function databaseSetup(){
		global $parameters;	
		// sciezka do folderu z baza platnosci
		tools::_add_folder($parameters['platnosci']['host']);
		$database = str_replace('www.','',$_SERVER['HTTP_HOST']).".db";
		// parametry instalacji bazy
		$paramPlatnosci = array('drivers'=>array('driver'=>'sqlite'),'sqlite'=>array('host'=>$parameters['platnosci']['host'], 'database'=>$database ));
		$this->db = config::getDB($paramPlatnosci);
		$parameters['sqlite']['host'] = $parameters['platnosci']['host'];
		$parameters['drivers']['dbinstall'] = $parameters['platnosci']['dbinstall'];
		config::_instal_database($parameters['drivers']['driver'],$this->db);
	}
	
	/**
	 * obsluga zdazen dla administratora
	 * @param $param
	 * @return unknown_type
	 */
	public function indexAction($param){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		switch($this->view){
			case 'platnosci_uzytkownika':
				tools::_assign("platnik",$this->user->all);
				$nowe_ = $this->listaPlatnosci( $this->user->id,1);
				if(is_array($nowe_))tools::_assign("nowe",$nowe_);
				tools::_display("Platnosci/dane_platnika.tpl");				
				break;

			default:					
				tools::_assign("users",UsersModel::get_registred_users_list());
				tools::_display("Platnosci/lista_uzytkownikow.tpl");
				break;
		}
		
	}
	
	/**
	 * obsluga zdazen dla uzytkownika
	 * @return unknown_type
	 */
	public function mojePlatnosciAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		switch($this->view){
			case 'zaplac_przez_dotpay':
				tools::_assign("platnik",$objSession->GetUserObject()->all);
				tools::_assign("tranzakacja",$this->jednaPlatnosc($this->plID));
				tools::_display("Platnosci/nowa_tranzakcja.tpl");
				break;	
							
			default:
				tools::_assign("platnik",$objSession->GetUserObject()->all);
				$nowe =  $this->listaPlatnosci( $objSession->GetUserObject()->id,1);
				if(is_array($nowe)) tools::_assign("nowe",$nowe);
				tools::_display("Platnosci/moje_platnosci.tpl");		
		}
	}
	
	public function dodajNowaAction(){
		global $objSession,$config;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$plID = $this->zapiszNowaTranzakcje();
		tools::_redirect("moje_platnosci,Platnosci,zaplacPrzezDotPay,{$plID}.html",null,0,'php');	
	}
	
	public function WybranyUzytkownikAction($id){
		$this->view = "platnosci_uzytkownika";	
		$this->user = new UserController($id);
	}
	
	public function zaplacPrzezDotPayAction($plID=null){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$this->view = "zaplac_przez_dotpay";
		$this->plID = $plID;
	}
	
	public function DotPayAction(){
		echo 'OK';
		exit;
	}
}
?>